using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace WinChat02
{
    public enum ConnectionRole { CRNone, CRClient, CRServer };

    delegate void LogAdd(string s);

    public class nw_core
    {
        private Form1 RefMainForm;
        public nw_client client = null;
        public nw_server server = null;
        public ConnectionRole Role;


        LogAdd log;
        
        public nw_core(Form1 Window) 
        {
            RefMainForm = Window;
            log = new LogAdd(RefMainForm.LogAdd);

        }

        public void StartChat(ConnectionRole cr) 
        {
            Role = cr;

            switch (cr) 
            {
                case ConnectionRole.CRServer:
                    server = new nw_server(RefMainForm);
                    server.Start();
                    

                    log("Starting as Server");
                    break;
                case ConnectionRole.CRClient:
                    client = new nw_client(RefMainForm);
                    client.Start_Client();

                    log("Starting as Client");
                    break;
                default:
                    return;
            }
        }

        public void SendMsg(string s) 
        {
            if (Role == ConnectionRole.CRServer) server.SendMsg(s);
            if (Role == ConnectionRole.CRClient) client.SendMsg(s);

        }

        public void ReceiveThreadMsg() 
        {

            if (Role == ConnectionRole.CRServer) 
            { 
                Thread t1 = new Thread(new ThreadStart(server.ReceiveThread));
                t1.Start();
            }
            else
            {
                Thread t1 = new Thread(new ThreadStart(client.ReceiveThread));
                t1.Start();
            
            }
        }


    }
}
